//
//  VisitorView.swift
//  xinlangweibo
//
//  Created by yangshuo on 15/11/23.
//  Copyright © 2015年 yangshuo. All rights reserved.
//

import UIKit
import SnapKit
//监听事件首要想到的是代理
//设置代理协议
protocol VisitorViewDelegate: NSObjectProtocol{
    //声明需要代理监听的方法
    func visitorViewDidRegister()//
    func visitorViewDidLogin()
}

class VisitorView: UIView {
    //创建代理属性(代理一定要使用weak来防止循环引用)
    weak var delegate: VisitorViewDelegate?
    //调用代理方法
    @objc private func clickRegister(){//func的格式:方法名()
        delegate?.visitorViewDidRegister()
    }
    @objc private func clickLogin(){
        delegate?.visitorViewDidLogin()
    }
    
    //MARK: 设置视图信息
    //设置访客视图
    func setupInfo(imageName: String?, title: String){
        message.text = title
        //对图片名进行判断,如果是nil那么代表首页return
        guard let imgName = imageName else{
            startAnim()
            return
        }
        iconView.image = UIImage(named: imgName)
        house.hidden = true
        sendSubviewToBack(maskIconView)
    }
    
    //开启动画
    //一直不停的旋转,需要想到核心动画
    private func startAnim(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI //变化值
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        //用在不断重复的动画上,避免重新运行时,动画停止播放.只有与动画绑定的图层销毁,动画自动销毁
        anim.removedOnCompletion = false
        
        //需要将动画添加到图层,否则无法播放
        iconView.layer .addAnimation(anim, forKey: nil)
    }
    
    //设置界面在自定义view时,通常要重写构造函数, initWithFrame是UIView指定构造函数 
    //补充一下,button中如果有文字的话,会自动设置button的frame
    //纯代码开发
    override init(frame: CGRect) {
        //实例化
        super.init(frame: frame)
        setupUI()
    }
    //通过SB与XIB开发
    required init?(coder aDecoder: NSCoder) {
        //阻止使用XIB与SB开发
       // fatalError("init(coder:) has not been implemented")
        //使用XIB与SB开发,coder是外部函数,adecoder内部函数
        super.init(coder: aDecoder)
        setupUI()
    }
    //未登录前,每个页面有4个控件,一张imageview,一个label,2个button,首页额外一个旋转动画
    //懒加载控件,旋转图标
    //图标 使用image构造函数创建的Imageview默认大小就是image
    private lazy var iconView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
    //遮罩
    private lazy var maskIconView : UIImageView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
    //小房子
    private lazy var house : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //懒加载消息文字
    private lazy var message: UILabel = UILabel(title: "label初始化文字", fontSize: 14)    //抽取便利构造函数的原则(代码重构原则) :相同的代码最好不要出现两次, 如果两段代码有很大的共性需要考虑抽取
    //注册按钮registerButton
    private lazy var registerButton: UIButton = UIButton(title: "注册", color: UIColor.orangeColor(), backImageName: "common_button_white_disable")    //登录按钮
    private lazy var loginButton: UIButton = UIButton(title: "登录", color: UIColor.redColor(), backImageName: "common_button_white_disable")
    
    
    }
//MARK: 设置界面
extension VisitorView{
    private func setupUI(){
        //添加子控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(house)
        addSubview(message)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //设置自动布局
        //添加约束需要添加到父视图上
        //建议:子视图有一个统一参照物(就是一个控件来参照父视图,其余控件的位置参照该控件!)
        
        //translatesAutoresizingMaskIntoConstraints 默认是true, 支持使用setFrame的方式设置控件位置
        //false支持使用自动布局来设置控件位置
        
        // iconView.translatesAutoresizingMaskIntoConstraints = false
        //使用forin遍历来快速subviews
        for s in subviews{
            s.translatesAutoresizingMaskIntoConstraints = false
        }
        
        //使用snapKit方法来添加约束
        //旋转图标
        iconView.snp_makeConstraints { (make) -> Void in
            //指定->center.x 属性 等于'参照对象''snp_'参照属性值
            //offset为相对试图约束的偏移量
            //snp_makecontranints创建约束,不要忘记参照对象的参照属性添加snp_
         make.centerX .equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-60)
        }
        //小房子
        house.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconView.snp_center)
        }
        //message
        message.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(16)
            //指定宽度
            make.width.equalTo(224)
            make.height.equalTo(36)
        }
        //注册按钮
        registerButton.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(message.snp_left)
            make.top.equalTo(message.snp_bottom).offset(16)
            make.width.equalTo(100)
            make.height.equalTo(36)
        }
        //登陆按钮
        loginButton.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(message.snp_right)
            make.top.equalTo(registerButton.snp_top)
            make.width.equalTo(registerButton.snp_width)
            make.height.equalTo(registerButton.snp_height)
        }
        //添加旋转图标约束
//        //toItem: self 是代表visitorView
//        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60))
        
//        //添加遮罩
////        addConstraint(NSLayoutConstraint(item: maskIconView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
////        addConstraint(NSLayoutConstraint(item: maskIconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: 0))
//        //添加小房子
//        addConstraint(NSLayoutConstraint(item: house, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: house, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1.0, constant: 0))
//        //添加message
//        addConstraint(NSLayoutConstraint(item: message, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: message, attribute: .Top, relatedBy: .Equal, toItem: iconView, attribute: .Bottom, multiplier: 1.0, constant: 16))
//        addConstraint(NSLayoutConstraint(item: message, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
//        addConstraint(NSLayoutConstraint(item: message, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
//
//        //添加按钮
//        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Left, relatedBy: .Equal, toItem: message, attribute: .Left, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: message, attribute: .Bottom, multiplier: 1.0, constant: 16))
//        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
//        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
//        //登陆按钮
//        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Right, relatedBy: .Equal, toItem: message, attribute: .Right, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: message, attribute: .Bottom, multiplier: 1.0, constant: 16))
//        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
//        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
//        
        //遮罩snpkit约束
        maskIconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
            make.bottom.equalTo(registerButton.snp_bottom)
        }
        
//        /**VFL语言:可视化格式语言
//                addConstraints(NSLayoutConstraint.!!
//          H:水平方向
//        V:垂直方向
//        |:边界
//        [];控件
//       "V:|-0-[mask]-(36)-[resbutton]" 设置数值时,使用小括号
//        views:是一个字典[名字;控件名] -VFL 字符串中表示控件的字符串
//        metries:是一个字典[名字:控件名] -VFL 字符串中表示某一个数值
//        options通常设置[]
//        
//*/
//        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[mask]-0-|", options: [], metrics: nil, views: ["mask": maskIconView]))
//        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[mask]-(btnHeight)-[resbutton]", options: [], metrics: ["btnHeight": -36], views: ["mask": maskIconView, "resbutton": registerButton]))
        //设置背景颜色(swift中不需要写self.XX)
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
        //实现监听的方法
        registerButton.addTarget(self, action: "clickRegister", forControlEvents: UIControlEvents.TouchUpInside)
        loginButton.addTarget(self, action: "clickLogin", forControlEvents: UIControlEvents.TouchUpInside)
        
    }
    
}

